package com.sskj.wallet.ui.activity;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.ChongTiRecordBean;
import com.sskj.wallet.presenter.RecordChongtiPresenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.WALLET_ACTIVITY_RECORD_CHONGTI)
public class RecordChongtiActivity extends BaseActivity<RecordChongtiPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @Autowired
    String recordType;

    private BaseAdapter<ChongTiRecordBean> adapter;

    private int page = 1;
    private int size = 10;

    private Map<String, String> rechargeStatus;
    private Map<String, String> withDrawStatus;


    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_record_chongti;
    }

    @Override
    public RecordChongtiPresenter getPresenter() {
        return new RecordChongtiPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        if (recordType.equals(Constans.WITHDRAW)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.wallet_recordChongtiActivity1));
        } else if (recordType.equals(Constans.RECHARGE)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.wallet_recordChongtiActivity2));
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setFirstDraw(false)
                .setPaintColor(Color.TRANSPARENT));
        rechargeStatus = new HashMap<>();
        rechargeStatus.put("1", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity3));
        rechargeStatus.put("2", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity4));
        withDrawStatus = new HashMap<>();
        withDrawStatus.put("1", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity5));
        withDrawStatus.put("2", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity6));
        withDrawStatus.put("3", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity7));
        withDrawStatus.put("4", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity8));
        withDrawStatus.put("5", App.INSTANCE.getString(R.string.wallet_recordChongtiActivity9));
        adapter = new BaseAdapter<ChongTiRecordBean>(R.layout.wallet_item_record_chongti, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, ChongTiRecordBean item) {
                holder.setText(R.id.tv_type_address, recordType.equals(Constans.WITHDRAW) ? App.INSTANCE.getString(R.string.wallet_addressListActivity1) : App.INSTANCE.getString(R.string.wallet_recordChongtiActivity11))
                        .setText(R.id.tv_type_amount, recordType.equals(Constans.WITHDRAW) ? App.INSTANCE.getString(R.string.wallet_recordChongtiActivity12) : App.INSTANCE.getString(R.string.wallet_recordChongtiActivity13))
                        .setText(R.id.tv_address, item.getQianbao_url())
                        .setText(R.id.tv_amount, item.getPrice())
                        .setText(R.id.tv_addtime, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_checktime, isEmpty(item.getCheck_time()) ? "" : TimeFormatUtil.formatE(Long.valueOf(item.getCheck_time()) * 1000))
                        .setText(R.id.tv_memo, recordType.equals(Constans.WITHDRAW) ? withDrawStatus.get(item.getState()) : rechargeStatus.get(item.getState()));
            }
        };
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener((recyclerView, position, v) -> {
            if (adapter.getData().size() == 0) {
                return;
            }
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_DETAILS_CHONGTI)
                    .withString(Constans.RECORD_TYPE, recordType)
                    .withString(Constans.RECORD_ID, adapter.getData().get(position).getPid())
                    .navigation();
        });
    }

    @Override
    public void initData() {
        mPresenter.getRecord(true, "", recordType, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getRecord(false, "", recordType, page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getRecord(false, "", recordType, page, size);
        });
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }

    public void getRecordOK(List<ChongTiRecordBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }

    }
}
